package com.san.base.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.domain.CodeDO;
import com.san.base.service.CodeService;
import com.san.base.vo.CodeVO;
import com.san.common.annotation.Log;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;

/**
 * 数据编码表
 * @author SuperSR
 * @date 2019年2月20日 下午2:18:05
 *
 */
@Controller
@RequestMapping("/base/code")
public class CodeController extends BaseController{

	/**
	 * 本功能地址前缀
	 */
	private static final String PREFIX = "base/code";
	//private static final Logger logger = LoggerFactory.getLogger(CodeController.class);
	
	@Autowired
	private CodeService service;

	@GetMapping("")
	@RequiresPermissions("base:code:view")
	String code(){
		return PREFIX + "/view";
	}
	
	/**
	 * 查询所有的系统编码
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("base:code:view")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<CodeDO> codeList = service.list(query);
		int total = service.count(query);
		PageUtils pageUtils = new PageUtils(codeList, total);
		return pageUtils;
	}
	
	/**
	 * 进入新增页面
	 * @return
	 */
	@GetMapping("/add")
	@RequiresPermissions("base:code:add")
	String add() {
		return PREFIX + "/add";
	}
	
	/**
	 * 编辑系统编码
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions("base:code:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		CodeDO code = service.getById(id);
		model.addAttribute("code", code);
		return PREFIX + "/edit";
	}
	
	/**
	 * 保存新增系统编码
	 * @param code
	 * @return
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("base:code:add")
	@Log("新增系统编码")
	public R save(CodeVO code) {
		code.setCreateUserId(getUserId());
		int result = service.save(code);
		if(result == -1){
			return R.error("该系统编码已经存在");
		}else if (result > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 修改系统编码
	 * @param code
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doEdit")
	@RequiresPermissions("base:code:edit")
	@Log("修改系统编码")
	public R doEdit(CodeVO code) {
		
		code.setUpdateUserId(getUserId());
		int result = service.doEdit(code);
		if(result == -1){
			return R.error("该系统编码已经存在");
		}else if(result > 0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除系统编码
	 * @param id
	 * @return
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("base:code:remove")
	@Log("删除系统编码")
	public R remove(Long id) {
		if (service.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 批量删除系统编码
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("base:code:batchRemove")
	@Log("批量删除系统编码")
	public R remove(@RequestParam("ids[]") Long[] ids) {
		service.batchRemove(ids);
		return R.ok();
	}
	
}












